function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
};

function calcProdSubTotal() {
    
    var prodSubTotal = 0;

    $(".row-total-input").each(function(){
    
        var valString = $(this).val() || 0;
        
        prodSubTotal += parseFloat(valString);
                    
    });
        
    $("#product-subtotal").val(prodSubTotal);

};

function calcTotalPallets() {

    var totalPallets = 0;

    $(".num-pallets-input").each(function() {
    
        var thisValue = $(this).val();
    
        if ( (IsNumeric(thisValue)) &&  (thisValue != '') ) {
        
            totalPallets += parseFloat(thisValue);
        
        };
    
    });
    
    $("#total-pallets-input").val(totalPallets);

};

function calcShippingTotal() {

    var totalPallets = $("#total-pallets-input").val() || 0;
    var shippingRate = $("#shipping-rate").text() || 0;
    var shippingTotal = totalPallets * shippingRate;
    
    $("#shipping-subtotal").val(shippingTotal);

};

function calcOrderTotal() {

    var orderTotal = 0;

    var productSubtotal = $("#product-subtotal").val() || 0;
    var shippingSubtotal = $("#shipping-subtotal").val() || 0;
        
    var orderTotal = parseFloat(productSubtotal) + parseFloat(shippingSubtotal);
    var orderTotalNice = "$" + orderTotal;
    //院内设施
    //$("#order-total").val(orderTotalNice);
        
};
function fieldIsNumericAndNotNull(fieldObj){
    if ( (IsNumeric(fieldObj)) && (fieldObj != '') ){
        return true;
    }
    return false;
};

//(1)原房金额计算
function yuanfangTotalFee(){
    //原房金额
    var house_fees = 0;
    //设施金额
    var yuannei_total_fee = $("#yuannei_total_fee").val();
    //alert("hello"+yuannei_total_fee);
    var intColumns = 7;
    for (var intI=1;intI<=intColumns;intI++ )
    {
        var areaObj = $("#house_area"+intI).val();
        var priceObj = $("#house_price"+intI).val();
        var feeObj = $("#house_fee"+intI);

        if ( (IsNumeric(areaObj)) && (areaObj != '') ){
            if ( (IsNumeric(priceObj)) && (priceObj != '') ){
                feeObj.attr("value",areaObj * priceObj); 
                //原房金额
                house_fees  = house_fees + parseFloat(areaObj * priceObj);
            }
        }
    }
    //原房合计金额
    if ( (IsNumeric(yuannei_total_fee)) && (yuannei_total_fee != '') ){
        $("#oldhouse_total_fee").val(house_fees + parseFloat(yuannei_total_fee));
    }else{
        $("#oldhouse_total_fee").val(house_fees);
    }

    //alert( $("#yuannei_total_fee").val());
};
//(2)新房金额计算
function xinfangTotalFee(){
    //总费用
    var newhouse_total_fee = 0
    //面积
    var duiding_area = $("#duiding_area").val();
    var yingfen_area = $("#yingfen_area").val();
    var zengdang_area = $("#zengdang_area").val();
    var chaodang_area = $("#chaodang_area").val();
    var other_area = $("#other_area").val();

    //价格
    var duiding_price = $("#duiding_price").val();
    var yingfen_price = $("#yingfen_price").val();
    var zengdang_price = $("#zengdang_price").val();
    var chaodang_price = $("#chaodang_price").val();
    var other_price = $("#other_price").val();
    //金额
    if(fieldIsNumericAndNotNull(duiding_area) && fieldIsNumericAndNotNull(duiding_price)){
        $("#duiding_fee").val(duiding_area * duiding_price);
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#duiding_fee").val());
    }
    if(fieldIsNumericAndNotNull(yingfen_area) && fieldIsNumericAndNotNull(yingfen_price)){
        $("#yingfen_fee").val(yingfen_area * yingfen_price);
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#yingfen_fee").val());
    }
    if(fieldIsNumericAndNotNull(zengdang_area) && fieldIsNumericAndNotNull(zengdang_price)){
        $("#zengdang_fee").val(zengdang_area * zengdang_price);
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#zengdang_fee").val());
    }
    if(fieldIsNumericAndNotNull(chaodang_area) && fieldIsNumericAndNotNull(chaodang_price)){
        $("#chaodang_fee").val(chaodang_area * chaodang_price);
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#chaodang_fee").val());
    }
    if(fieldIsNumericAndNotNull(other_area) && fieldIsNumericAndNotNull(other_price)){
        $("#other_fee").val(other_area * other_price);
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#other_fee").val());
    }

    //增加部分
    var duiding_diff = $("#duiding_diff").val();
    var yingfen_diff = $("#yingfen_diff").val();
    var zengdang_diff = $("#zengdang_diff").val();
    var chaodang_diff = $("#chaodang_diff").val();
    var other_diff = $("#other_diff").val();
    if(fieldIsNumericAndNotNull(duiding_diff) && fieldIsNumericAndNotNull(duiding_diff)){
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#duiding_diff").val());
    }
    if(fieldIsNumericAndNotNull(yingfen_diff) && fieldIsNumericAndNotNull(yingfen_diff)){
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#yingfen_diff").val());
    }
    if(fieldIsNumericAndNotNull(zengdang_diff) && fieldIsNumericAndNotNull(zengdang_diff)){
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#zengdang_diff").val());
    }
    if(fieldIsNumericAndNotNull(chaodang_diff) && fieldIsNumericAndNotNull(chaodang_diff)){
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#chaodang_diff").val());
    }
    if(fieldIsNumericAndNotNull(other_diff) && fieldIsNumericAndNotNull(other_diff)){
        newhouse_total_fee = newhouse_total_fee + parseFloat($("#other_diff").val());
    }
    //减少部分
    var duiding_diff2 = $("#duiding_diff2").val();
    var yingfen_diff2 = $("#yingfen_diff2").val();
    var zengdang_diff2 = $("#zengdang_diff2").val();
    var chaodang_diff2 = $("#chaodang_diff2").val();
    var other_diff2 = $("#other_diff2").val();
    if(fieldIsNumericAndNotNull(duiding_diff2) && fieldIsNumericAndNotNull(duiding_diff2)){
        newhouse_total_fee = newhouse_total_fee - parseFloat($("#duiding_diff2").val());
    }
    if(fieldIsNumericAndNotNull(yingfen_diff2) && fieldIsNumericAndNotNull(yingfen_diff2)){
        newhouse_total_fee = newhouse_total_fee - parseFloat($("#yingfen_diff2").val());
    }
    if(fieldIsNumericAndNotNull(zengdang_diff2) && fieldIsNumericAndNotNull(zengdang_diff2)){
        newhouse_total_fee = newhouse_total_fee - parseFloat($("#zengdang_diff2").val());
    }
    if(fieldIsNumericAndNotNull(chaodang_diff2) && fieldIsNumericAndNotNull(chaodang_diff2)){
        newhouse_total_fee = newhouse_total_fee - parseFloat($("#chaodang_diff2").val());
    }
    if(fieldIsNumericAndNotNull(other_diff2) && fieldIsNumericAndNotNull(other_diff2)){
        newhouse_total_fee = newhouse_total_fee - parseFloat($("#other_diff2").val());
    }

    //新房总金额
    $("#newhouse_total_fee").val(newhouse_total_fee);
}
//(3)最后结算费用
function jiesuanTotalFee(){
    //其他费用
    var other_cost_fee = $("#other_cost_fee").val();
    var oldhouse_total_fee = $("#oldhouse_total_fee").val();
    var newhouse_total_fee = $("#newhouse_total_fee").val();

    var jiesuanTotal = parseFloat(newhouse_total_fee)-parseFloat(oldhouse_total_fee);
    if(fieldIsNumericAndNotNull(other_cost_fee) && fieldIsNumericAndNotNull(other_cost_fee)){
        jiesuanTotal = jiesuanTotal - parseFloat(other_cost_fee);
        //控制"余"、"欠"radio
        if(jiesuanTotal>0){
            //setCheckboxValue("enouph_flag","0","formEditor");
        }else{
            //setCheckboxValue("enouph_flag","1","formEditor");
        }
    }
    $("#jiesuan_fee").val(Math.abs(jiesuanTotal));
};
//大写赋值
function getUpperStr(numberStr){
    var CN_ZERO = "零";
    var CN_ONE = "壹";
    var CN_TWO = "贰";
    var CN_THREE = "叁";
    var CN_FOUR = "肆";
    var CN_FIVE = "伍";
    var CN_SIX = "陆";
    var CN_SEVEN = "柒";
    var CN_EIGHT = "捌";
    var CN_NINE = "玖";
    var CN_TEN = "拾";
    var CN_HUNDRED = "佰";
    var CN_THOUSAND = "仟";
    var CN_TEN_THOUSAND = "万";
    var CN_HUNDRED_MILLION = "亿";
    var CN_SYMBOL = "人民币";
    var CN_DOLLAR = "元";
    var CN_TEN_CENT = "角";
    var CN_CENT = "分";
    var CN_INTEGER = "整";
    if (numberStr == null || numberStr =="")
    {
        return CN_ZERO;
    }

    if (numberStr == "0") return CN_ZERO;
    if (numberStr == "1") return CN_ONE;
    if (numberStr == "2") return CN_TWO;
    if (numberStr == "3") return CN_THREE;
    if (numberStr == "4") return CN_FOUR;
    if (numberStr == "5") return CN_FIVE;
    if (numberStr == "6") return CN_SIX;
    if (numberStr == "7") return CN_SEVEN;
    if (numberStr == "8") return CN_EIGHT;
    if (numberStr == "9") return CN_NINE;
    if (numberStr == "10") return CN_TEN;
    if (numberStr == "100") return CN_HUNDRED;
    if (numberStr == "1000") return CN_THOUSAND;
};
function jiesuanFeeUpper(){
    var jiesuan_fee = $("#jiesuan_fee").val() || '';

    var pointLocation = jiesuan_fee.indexOf(".");
    var intLength = jiesuan_fee.length;
    //alert("intLength:"+intLength+"\n pointLocation:"+pointLocation);

    //整数长度
    intInterger = 0;
    if(pointLocation == -1){//没有小数点
        $("#jiesuan_daxie_fen").val(getUpperStr("0"));
        $("#jiesuan_daxie_jiao").val(getUpperStr("0"));

        intInterger = intLength;
    }else
    {
        $("#jiesuan_daxie_fen").val(getUpperStr(jiesuan_fee.substring(pointLocation+2,pointLocation+3)));
        $("#jiesuan_daxie_jiao").val(getUpperStr(jiesuan_fee.substring(pointLocation+1,pointLocation+2)));

        intInterger = pointLocation;
    }


    $("#jiesuan_daxie_yuan").val(getUpperStr(jiesuan_fee.substring(intInterger-1,intInterger)));
    $("#jiesuan_daxie_shi").val(getUpperStr(jiesuan_fee.substring(intInterger-2,intInterger-1)));
    $("#jiesuan_daxie_bai").val(getUpperStr(jiesuan_fee.substring(intInterger-3,intInterger-2)));
    $("#jiesuan_daxie_qian").val(getUpperStr(jiesuan_fee.substring(intInterger-4,intInterger-3)));
    var wanStr = "";


    if (intInterger ==5)
    {
        wanStr = getUpperStr(jiesuan_fee.substring(intInterger-5,intInterger-4));
    }else if (intInterger ==6)
    {
        wanStr = getUpperStr(jiesuan_fee.substring(intInterger-6,intInterger-5))+getUpperStr("10")+getUpperStr(jiesuan_fee.substring(intInterger-5,intInterger-4));
    }else if (intInterger ==7)
    {
        wanStr = getUpperStr(jiesuan_fee.substring(intInterger-7,intInterger-6))+getUpperStr("100")+getUpperStr(jiesuan_fee.substring(intInterger-6,intInterger-5))+getUpperStr("10")+getUpperStr(jiesuan_fee.substring(intInterger-5,intInterger-4));
    }
    else if (intInterger ==8)
    {
        wanStr = getUpperStr(jiesuan_fee.substring(intInterger-8,intInterger-7))+getUpperStr("1000")+getUpperStr(jiesuan_fee.substring(intInterger-7,intInterger-6))+getUpperStr("100")+getUpperStr(jiesuan_fee.substring(intInterger-6,intInterger-5))+getUpperStr("10")+getUpperStr(jiesuan_fee.substring(intInterger-5,intInterger-4));
    }else if (intInterger >8)
    {
        wanStr = "不合理数据";
    }
    $("#jiesuan_daxie_wan").val(wanStr);

};

$(function(){

    $('.num-pallets-input').blur(function(){
    
        var $this = $(this);
    
        var numPallets = $this.val();
        var multiplier = $this
                            .parent().parent()
                            .find("td.price-per-pallet span")
                            .text();
        
        if ( (IsNumeric(numPallets)) && (numPallets != '') ) {
            
            var rowTotal = numPallets * multiplier;
            
            $this
                .css("background-color", "white")
                .parent().parent()
                .find("td.row-total input")
                .val(rowTotal);                    
            
        } else {
        
            $this.css("background-color", "#ffdcdc"); 
                        
        };
        
//        calcProdSubTotal();
//        calcTotalPallets();
//        calcShippingTotal();
//        calcOrderTotal();

        yuanfangTotalFee();
        xinfangTotalFee();
        jiesuanTotalFee();
        jiesuanFeeUpper();
    
    });

});